package com.coolgor.coldot.web.wechat.gzh;

import com.coolgor.coldot.common.HttpServletRequestUtil;

import com.coolgor.coldot.entity.wechat.gzh.GZHMessageTemplate;
import com.coolgor.coldot.dto.wechat.gzh.GZHMessageTemplateExecution;
import com.coolgor.coldot.enums.wechat.gzh.GZHMessageTemplateOperationStateEnum;
import com.coolgor.coldot.service.wechat.gzh.GZHMessageTemplateService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;


@RestController  /* @Controller + @ResponseBody */
@RequestMapping(value="/GZHMessageTemplate", produces="application/json;charset=utf-8", method={RequestMethod.GET,RequestMethod.POST})
public class GZHMessageTemplateController {

	private static Logger log = LoggerFactory.getLogger(GZHMessageTemplateController.class);

	@Autowired
	private GZHMessageTemplateService gZHMessageTemplateService;

	@RequestMapping(value = "/getByID", method={RequestMethod.GET,RequestMethod.POST})
	private Map<String, Object> getByID(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long recordID = HttpServletRequestUtil.getLong(request, "recordid");
		if (recordID > -1) {
			try {
				GZHMessageTemplateExecution exe = gZHMessageTemplateService.getByID(recordID); 
				if ( exe.getState() == GZHMessageTemplateOperationStateEnum.SUCCESS.getState() ) {
					modelMap.put("success", true);
					modelMap.put("gzhmessagetemplate", exe.getGZHMessageTemplate());
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", exe.getStateInfo());
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty recordid");
		}
		return modelMap;
	}


	@RequestMapping(value = "/getList", method={RequestMethod.GET,RequestMethod.POST})
	private Map<String, Object> getList(@RequestBody GZHMessageTemplate gZHMessageTemplateCondition, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		int page = HttpServletRequestUtil.getInt(request, "page");
		int pageSize = HttpServletRequestUtil.getInt(request, "rows");
		if (gZHMessageTemplateCondition != null ) {
			try {
				GZHMessageTemplateExecution exe = gZHMessageTemplateService.getList(gZHMessageTemplateCondition, page, pageSize); 
				if ( exe.getState() == GZHMessageTemplateOperationStateEnum.SUCCESS.getState() ) {
					modelMap.put("success", true);
					modelMap.put("total",exe.getCount());
					modelMap.put("rows",exe.getGZHMessageTemplateList());
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", exe.getStateInfo());
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty gZHMessageTemplateCondition");
		}
		return modelMap;
	}


	@RequestMapping(value = "/add", method={RequestMethod.GET,RequestMethod.POST})
	private Map<String, Object> add(@RequestBody GZHMessageTemplate gZHMessageTemplate, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (gZHMessageTemplate != null ) {
			try {
				GZHMessageTemplateExecution exe = gZHMessageTemplateService.add(gZHMessageTemplate); 
				if ( exe.getState() == GZHMessageTemplateOperationStateEnum.SUCCESS.getState() ) {
					modelMap.put("success", true);
					modelMap.put("gzhmessagetemplate", exe.getGZHMessageTemplate());
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", exe.getStateInfo());
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty gZHMessageTemplate");
		}
		return modelMap;
	}


	@RequestMapping(value = "/modify", method={RequestMethod.GET,RequestMethod.POST})
	private Map<String, Object> modify(@RequestBody GZHMessageTemplate gZHMessageTemplate, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (gZHMessageTemplate != null ) {
			try {
				GZHMessageTemplateExecution exe = gZHMessageTemplateService.modify(gZHMessageTemplate); 
				if ( exe.getState() == GZHMessageTemplateOperationStateEnum.SUCCESS.getState() ) {
					modelMap.put("success", true);
					modelMap.put("gzhmessagetemplate", exe.getGZHMessageTemplate());
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", exe.getStateInfo());
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty gZHMessageTemplate");
		}
		return modelMap;
	}


	@RequestMapping(value = "/remove", method={RequestMethod.GET,RequestMethod.POST})
	private Map<String, Object> remove(@RequestParam("ids") Long[] ids, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		/*
		String idString = HttpServletRequestUtil.getString(request, "ids");
		List<Long> list = new ArrayList<Long>();
		if (!"".equals(idString) && idString!=null) {
			String[] ids = idString.split(",");
			for (int i=0; i<ids.length; i++)  {
				list.add(Long.valueOf(ids[i]));
			}
			// 这里写执行的语句
		}
		*/
		if (ids != null && ids.length > 0) {
			List<Long> idList = new ArrayList<Long>();
			for (int i=0; i<ids.length; i++) {
				idList.add(ids[i]);
			}
			try {
				GZHMessageTemplateExecution exe = gZHMessageTemplateService.removeBatchByID(idList); 
				if ( exe.getState() == GZHMessageTemplateOperationStateEnum.SUCCESS.getState() ) {
					modelMap.put("success", true);
					modelMap.put("total",exe.getCount());
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", exe.getStateInfo());
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty id list !");
		}
		return modelMap;
	}


}
